package com.lastproject.ejb.util;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.TraineeDTO;
import com.lastproject.ejb.dao.RoleDAORemote;
import com.lastproject.ejb.entities.ConfidentialEmployee;
import com.lastproject.ejb.entities.Role;
import com.lastproject.ejb.entities.Trainee;

/**
 * Session Bean implementation class TraineeConversion Author: Alexandra
 */
@Stateless
@LocalBean
public class TraineeConversion {

	@PersistenceContext
	EntityManager entityManager;

	public TraineeConversion() {

	}

	public TraineeDTO fromEntity(Trainee trainee) {

		TraineeDTO traineeDTO = new TraineeDTO();

		traineeDTO.setId(trainee.getId());
		traineeDTO.setUsername(trainee.getUsername());
		traineeDTO.setPassword(trainee.getPassword());
		traineeDTO.setStatusPassword(trainee.getStatusPassword());
		traineeDTO.setFirstname(trainee.getFirstname());
		traineeDTO.setLastname(trainee.getLastname());
		traineeDTO.setDob(trainee.getDob());
		traineeDTO.setPersonalEmail(trainee.getPersonalEmail());
		traineeDTO.setPersonalPhone(trainee.getPersonalPhone());
		traineeDTO.setEmploymentDate(trainee.getEmploymentDate());
		traineeDTO.setDepartureDate(trainee.getDepartureDate());
		traineeDTO.setMedCheckUpDate(trainee.getMedCheckUpDate());
		traineeDTO.setEmploymentType(trainee.getEmploymentType());
		traineeDTO.setPosition(trainee.getPosition());
		traineeDTO.setCv(trainee.getCv());
		traineeDTO.setWorkEmail(trainee.getWorkEmail());
		traineeDTO.setSalary(trainee.getSalary());
		traineeDTO.setEnabled(trainee.isEnabled());
		traineeDTO.setPhoto(trainee.getPhoto());

		return traineeDTO;

	}

	public Trainee fromDTO(TraineeDTO traineeDTO) {

		System.out.println("TraineeCOonversion before fromDto "
				+ traineeDTO.getId());

		Trainee trainee = entityManager.find(Trainee.class, traineeDTO.getId());

		System.out.println("TraineeCOonversio after fromDto " + trainee);

		if (trainee == null) {
			trainee = new Trainee();
		}

		trainee.setId(traineeDTO.getId());
		trainee.setUsername(traineeDTO.getUsername());
		trainee.setPassword(traineeDTO.getPassword());
		trainee.setStatusPassword(traineeDTO.getStatusPassword());
		trainee.setFirstname(traineeDTO.getFirstname());
		trainee.setLastname(traineeDTO.getLastname());
		trainee.setDob(traineeDTO.getDob());
		trainee.setPersonalEmail(traineeDTO.getPersonalEmail());
		trainee.setPersonalPhone(traineeDTO.getPersonalPhone());
		trainee.setEmploymentDate(traineeDTO.getEmploymentDate());
		trainee.setDepartureDate(traineeDTO.getDepartureDate());
		trainee.setMedCheckUpDate(traineeDTO.getMedCheckUpDate());
		trainee.setEmploymentType(traineeDTO.getEmploymentType());
		trainee.setPosition(traineeDTO.getPosition());
		trainee.setCv(traineeDTO.getCv());
		trainee.setWorkEmail(traineeDTO.getWorkEmail());
		trainee.setSalary(traineeDTO.getSalary());
		trainee.setEnabled(traineeDTO.isEnabled());
		trainee.setPhoto(traineeDTO.getPhoto());

		return trainee;

	}

	public List<Role> convertRolesFromDTO(TraineeDTO traineeDTO) {

		List<Role> roleList = new ArrayList<Role>();

		for (int i = 0; i < traineeDTO.getRolesIdList().size(); i++) {
			System.out.println(i + " iterations so far");
			Role auxRole = entityManager.find(Role.class, traineeDTO
					.getRolesIdList().get(i));
			System.out.println("in iteration " + i + " role is"
					+ auxRole.getName());
			roleList.add(auxRole);
		}

		return roleList;
	}

	public List<Integer> convertRolesFromEntity(Trainee trainee) {

		List<Integer> roleList = new ArrayList<>();

		for (int i = 0; i < trainee.getRoles().size(); i++) {
			int idRole = trainee.getRoles().get(i).getId();
			roleList.add(idRole);
		}

		return roleList;
	}

}
